using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
using ModelAPI;
using InterfaceAPI;



// 场景控制器
public class FirstController : MonoBehaviour,ISceneController,IUserAction{
    public LandModel Land_start;
    public LandModel Land_end;
    public WaterModel water;
    public BoatModel boat;
    public RoleModel[] roles;
    public UserGUI GUI;

    // 加载场景，生成水，起点和终点地面，船，牧师和恶魔各三人
    public void LoadResoureces(){
        water=new WaterModel();
        Land_start=new LandModel(1);
        Land_end=new LandModel(-1);
        boat=new BoatModel();
        roles=new RoleModel[6];
        for(int i=0;i<6;i++) {
            RoleModel role=new RoleModel((i<3)?"priest":"devil", Land_start.getEmptyPos());
            role.setName(((i<3)?"priest":"devil")+i);
            Land_start.addRole(role);
            roles[i]=role;
        }
    }
    
    // 移动船只的脚本
    public void moveBoat(){
        if (boat.empty() || GUI.remake_flag!=0) return;
        boat.boatMove();

        GUI.remake_flag=check();
    }

    // 移动角色的脚本
    public void moveRole(RoleModel role){
        if(GUI.remake_flag!=0) return;
        if(role.getOnBoat()==1) {
            boat.deleteRole(role.getName());
            role.setOnBoat(0);
            role.getRole().transform.parent=null;
            if(boat.getSide()==1) {
                role.roleMove(Land_start.getEmptyPos());
                role.setSide(1);
                Land_start.addRole(role);
            }
            else {
                role.roleMove(Land_end.getEmptyPos());
                role.setSide(-1);
                Land_end.addRole(role);
            }       
        }else{
            if(role.getSide()==1) {
                if(boat.getEmptyNumber()==-1 || role.getSide()!=boat.getSide()) return;
                Land_start.deleteRole(role.getName());
                role.roleMove(boat.getEmptyPos());
                role.getRole().transform.parent=boat.getBoat().transform;
                role.setOnBoat(1);
                boat.addRole(role);
            }else{
                if(boat.getEmptyNumber() == -1 || role.getSide()!=boat.getSide()) return;
                Land_end.deleteRole(role.getName());
                role.roleMove(boat.getEmptyPos());
                role.getRole().transform.parent = boat.getBoat().transform;
                role.setOnBoat(1);
                boat.addRole(role);
            }
        }
        GUI.remake_flag=check();
    }

    //重开
    public void remake(){
        SceneManager.LoadScene(0);
    }
    
    // 检查胜负，胜利返回1，失败返回-1，还没结果返回0
    public int check(){
        int[] boatRole=boat.getRoleNum();
        int[] startRole=Land_start.getRoleNum();
        int[] endRole=Land_end.getRoleNum();

        if(endRole[0]+endRole[1]==6) return 1;

        if(boat.getSide()==1){ 
            startRole[0]+=boatRole[0];
            startRole[1]+=boatRole[1];
        }else{
            endRole[0]+=boatRole[0];
            endRole[1]+=boatRole[1];
        }

        if((startRole[0]>0 && startRole[1]>startRole[0])||(endRole[0]>0 && endRole[1]>endRole[0]))
            return -1;
        return 0;
    }

    // 初始先创建导演对象，绑定GUI，加载场景
    void Start(){
        TheDirector director=TheDirector.getInstance();
        director.CurrentSceneController=this;
        GUI=gameObject.AddComponent<UserGUI>() as UserGUI;
        LoadResoureces();
    }
}

